package com.hejia.bulbul.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hejia.alauda.entity.Parameter;
import com.hejia.alauda.service.ConfigureService;



@Controller
@RequestMapping("setting/setbanner")
public class SetBannerController extends BaseController{
	
	@Resource
	private ConfigureService parameterService;
	
	
	/**
	 * setbanner列表
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String setBannerList(ModelMap model) {
		List<Parameter> list = parameterService
				.getParameters(Parameter.Category.Banner);
		model.put("list", list);
		return "/setting/setbanner";
	}
	
	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addBannerList(ModelMap model) {
		
		return "/setting/inputbanner";
	}
	
	/**
	 * 添加完保存
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String saveBannerList(Parameter parameter,ModelMap model,RedirectAttributes redirectAttributes) {
		
		parameter.setCreateDate(new Date());
		parameter.setModifyDate(new Date());
		parameter.setCategory(Parameter.Category.Banner);
		if(parameterService.saveParameter(parameter)){
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "Banner添加成功");
		}else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "Banner添加失败");
		}
		
		return "redirect:/setting/setbanner/list";
	}
	
	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateBannerList(@PathVariable Long id,ModelMap model) {
		Parameter parameter= parameterService.getParameterById(id);
		model.put("parameter", parameter);
		
		return "/setting/inputbanner";
	}
	
	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.POST)
	public String saveUpdateBannerList(@PathVariable Long id,Parameter parameter,ModelMap model,RedirectAttributes redirectAttributes) {
		
		Parameter parameter2 = parameterService.getParameterById(id);
		parameter2.setModifyDate(new Date());
		parameter2.setCode(parameter.getCode());
		parameter2.setValue(parameter.getValue());
		parameter2.setMemo(parameter.getMemo());
		
		if(parameterService.updateParameter(parameter2)){
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "Banner修改成功");
		}else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "Banner修改失败");
		}
		
		return "redirect:/setting/setbanner/list";
	}
	
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public String deleteBannerList(@PathVariable Long id,ModelMap model,RedirectAttributes redirectAttributes) {
		if(parameterService.deleteParameter(id)){
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "Banner删除成功");
		}else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "Banner删除失败");
		}
		
		return "redirect:/setting/setbanner/list";
	}
	
}
